//
//  WeeksGoalSetupWeekView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/11.
//

import Foundation
import MYModule

class GoalSetupWeekDayView: View {
    
    private var didSelectClosure: (([Int]) -> ())?

    override func makeUI() {
        super.makeUI()
            
        self.addSubview(titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(30)
        }
        
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(30)
            make.bottom.equalTo(-30)
        }
        
        self.addSubview(self.underscoreView)
        self.underscoreView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }

    public func bindModel(_  selectedIndexs: [Int]) {
        self.selectedIndexs = selectedIndexs
        
        self.selectedIndexs.forEach { index in
            let model = self.collectionDataSource[index - 1]
            model.isSelected = true
        }

        self.collectionView.reloadData()
    }
    
    public func returnDidselectClosure(closure: @escaping ([Int]) -> ()) {
        self.didSelectClosure = closure
    }
    
    
    //MARK: - lazy load
    
    private lazy var selectedIndexs: [Int] = {
        let indexs = [Int]()
        return indexs
    }()
    
    private lazy var collectionDataSource: [GoalSetupWeekDayModel] = {
        var array = [GoalSetupWeekDayModel]()
        let titles = ["unit_week_full_1".language, "unit_week_full_2".language, "unit_week_full_3".language, "unit_week_full_4".language, "unit_week_full_5".language, "unit_week_full_6".language, "unit_week_full_7".language]
        titles.forEach { title in
            let model = GoalSetupWeekDayModel(title: title)
            array.append(model)
        }
        return array
    }()
        
    private lazy var titleLabel: Label = {
        let label = Label()
        label.text = "goal_setting_exercise_day".language
        label.font = Fonts.systemFont(ofSize: 16)
        return label
    }()
    
    private lazy var collectionViewflowLayout: UICollectionViewFlowLayout = {
        let flowLayoutView = UICollectionViewFlowLayout()
        let width = (UIScreen.main.bounds.size.width-64)/7
        flowLayoutView.itemSize = CGSize(width: width, height: 30)
        flowLayoutView.scrollDirection = .vertical
        flowLayoutView.minimumLineSpacing = 4
        flowLayoutView.minimumInteritemSpacing = 0
        flowLayoutView.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return flowLayoutView
    }()
    
    private lazy var collectionView: CollectionView = {
        let collectionView = CollectionView(frame: .zero, collectionViewLayout: collectionViewflowLayout)
        collectionView.backgroundColor = UIColor.background
        collectionView.register(GoalSetupWeekDaysCollectionCell.self, forCellWithReuseIdentifier: GoalSetupWeekDaysCollectionCell.reuseIdentifier)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    private lazy var underscoreView: View = {
        let view = View()
        view.backgroundColor = .assist_D0D0D0
        return view
    }()
}

extension GoalSetupWeekDayView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.collectionDataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoalSetupWeekDaysCollectionCell.reuseIdentifier, for: indexPath) as! GoalSetupWeekDaysCollectionCell
        cell.bindModel(model: self.collectionDataSource[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.collectionDataSource[indexPath.row]
        model.isSelected = !model.isSelected
        if self.selectedIndexs.contains(indexPath.row + 1) {
            self.selectedIndexs.removeAll(indexPath.row + 1)
        }
        else {
            self.selectedIndexs.append(indexPath.row + 1)
        }
        
        if self.didSelectClosure != nil {
            self.didSelectClosure?(self.selectedIndexs)
        }
        collectionView.reloadData()
    }
}
